import {
  listComPersonnelInfo,
  getComPersonnelInfo,
  delComPersonnelInfo,
  addComPersonnelInfo,
  updateComPersonnelInfo,
} from "@/apis/user/personnelInfo";
import { getCookiesUserInfo } from "@/utils/auth";

export const api = {
  list: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await listComPersonnelInfo(queryParams);
    return res;
  },
  one: getComPersonnelInfo,
  delete: delComPersonnelInfo,
  add: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await addComPersonnelInfo(queryParams);
    return res;
  },
  update: updateComPersonnelInfo,
  download: (queryParams) => {
    this.download(
      "company/comPersonnelInfo/export",
      {
        ...queryParams,
      },
      `comPersonnelInfo_${new Date().getTime()}.xlsx`
    );
  },
};

const MetaData = [
  {
    title: "ID",
    dataIndex: "id",
    dataType: "Number",
    hidden: true,
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "姓名",
    dataIndex: "name",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "部门",
    dataIndex: "department",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "职位",
    dataIndex: "position",
    dataType: "Dict",
    inQuery: true,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "部门主管",
      },
      {
        value: 1,
        label: "负责人",
      },
      {
        value: 2,
        label: "一般成员",
      },
    ],
  },
  {
    title: "级别",
    dataIndex: "level",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "工作地点",
    dataIndex: "location",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "联系电话",
    dataIndex: "contactNumber",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "电子邮件",
    dataIndex: "email",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "不可用",
      },
      {
        value: 1,
        label: "可用",
      },
    ],
  },
];

export const queryItems = () => {
  let items = MetaData.filter((item) => {
    return item.inQuery;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    }
    return newItem;
  });
  return items;
};

export const columns = () => {
  let items = MetaData.filter((item) => {
    return item.inTable;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
      key: item.dataIndex,
      align: "center",
    };
    if (item.dataType == "Dict") {
      newItem.customRender = (text, record, index) => {
        let option = item.options.find((option) => {
          return option.value == record[item.dataIndex];
        });
        return option ? option.label : "-";
      };
    }
    return newItem;
  });
  items.push({
    title: "操作",
    dataIndex: "action",
    key: "action",
    align: "center",
    width: 100,
    scopedSlots: { customRender: "action" },
  });
  return items;
};

export const formItems = () => {
  let items = MetaData.filter((item) => {
    return item.inForm && !item.hidden;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    }
    return newItem;
  });
  return items;
};

export const rules = {
  name: [{ required: true, message: "必须输入姓名" }],
  department: [{ required: true, message: "必须输入部门" }],
  contactNumber: [{ required: true, message: "必须输入联系电话" }],
};

export const formConfg = function () {
  var config = {
    group: 2,
    labelCol: { span: 4 },
    wrapperCol: { span: 14 },
  };
  var items = formItems();

  // 计算分列
  let half = items.length / 2;
  let yu = items.length % 2;
  if (yu > 0) {
    half = half + 1;
  }
  config.formItems = [items.slice(0, half), items.slice(half, items.length)];
  return config;
};
